package com.dhh.sky.activity;

import java.util.Map;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
import com.dhh.sky.R;
import com.dhh.sky.service.MusicPlayerService;
import com.dhh.sky.util.Constant;
import com.dhh.sky.widget.SlideMenuFragment;
import com.dhh.sky.widget.SongListAdapter;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class SongListActivity extends SlidingFragmentActivity{
	private ListView listView;
	private SongListAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setBehindContentView(R.layout.menu_frame);
		setContentView(R.layout.playlist);
		initActionBar();
		
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, new SlideMenuFragment()).commit();
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
	}
	
	@Override
	public void onStart(){
		super.onStart();
		initView();
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem playerItem = menu.add(getString(R.string.ab_audio_player));
        playerItem.setIcon(R.drawable.ic_media_player);
        playerItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        playerItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
			@Override
			public boolean onMenuItemClick(MenuItem item) {	
				startActivity(new Intent(SongListActivity.this,AudioPlayerActivity.class));
				return true;
			}});
        
        if(isAudioServiceRunning()){
        	playerItem.setVisible(true);
        }else{
        	playerItem.setVisible(false);
        }
        
        return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
	        case android.R.id.home:     
	        	toggle();
				return true;
        }
    	
        return false;
    }
	
	private void initView(){
		listView = (ListView) this.findViewById(R.id.song_list);
		adapter = new SongListAdapter(this);
		listView.setTextFilterEnabled(true);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				Map<String,String> map = adapter.getItem(position); 
				String path = map.get(Constant.SONG_PATH);
				Intent intent = new Intent(SongListActivity.this,AudioPlayerActivity.class);
				intent.putExtra(Constant.PARAM_PATH, path);
				startActivity(intent);
			}});
	}
	
	private void initActionBar(){
		getSupportActionBar().setHomeButtonEnabled(true);    
    	getSupportActionBar().setIcon(R.drawable.ic_app_menu);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);  
        getSupportActionBar().setTitle(this.getString(R.string.title_play_list));
	}
	
	private boolean isAudioServiceRunning() {
	    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
	    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
	        if (MusicPlayerService.class.getName().equals(service.service.getClassName())) {
	            return true;
	        }
	    }
	    return false;
	}
}
